<?php
class Business_SaleCategory{
	/**
	 * 
	 * @return Array | Zend_Exception
	 */
	public static function getAllId(){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_getAllSaleCategoryId()');
			$stmt->execute();
			$result = $stmt->fetchAll();
			return $result;
		} catch (Zend_Exception $e) {
			
			return array();
		}
	}
	
	public static function getAllIds($ids){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_getSaleCategoryByIds(:p_ids)');
			$stmt->bindValue('p_ids',$ids);
			$stmt->execute();
			$result = $stmt->fetchAll();
			$stmt=null;
			return $result;
		} catch (Zend_Exception $e) {
			return $e->getMessage();
		}
	}
	
	/**
	 * 
	 * @param $params['key'] = SaleCategoryID,SaleCategoryName | $params['value'] = 1, Name
	 * @return Array
	 */
	public static function get($params){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_getSaleCategoryBy'.$params['key'].'(:s_'.$params['key'].')');
			$stmt->bindValue('s_'.$params['key'],$params['value']);
			$stmt->execute();
			$result = $stmt->fetchAll();
			unset($stmt);
			
			return $result;
		} catch (Zend_Exception $e) {
			return array();
		}
	}	
	

	public static function create($params){
		try {
			
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_insertSaleCategory(:s_catName,:s_catOrder)');
			$stmt->bindValue('s_catName',$params['catName']);
			$stmt->bindValue('s_catOrder',$params['catOrder']);
			$stmt->execute();
			$result = $stmt->rowCount();
			return $result;
		} catch (Zend_Exception $e) {
            var_dump($e->getMessage());exit;
			return 0;
		}
	}
	/**
	 * 
	 * @param unknown_type $params
	 * @return number
	 */
	public static function update($params){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_updateSaleCategory(:s_CatId,:s_CatName,:s_CatOrder)');
			$stmt->bindValue('s_CatId',$params['catId']);
			$stmt->bindValue('s_CatName',$params['catName']);
			$stmt->bindValue('s_CatOrder',$params['catOrder']);
			$stmt->execute();			
			
			$result = $stmt->rowCount();
			
			unset($stmt);
			
			return $result;
		} catch (Zend_Exception $e) {
			return 0;
		}
	}
	
	public static function delete($catId){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare("CALL sp_deleteSaleCategorybyIds(:s_CatId)");
			$stmt->bindValue('s_CatId',$catId);
			return $stmt->execute();
		} catch (Zend_Exception $e) {
			return $e->getMessage();
		}
	}
	
	public static function find($params){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare("CALL sp_findSaleCategory(:s_CatName)");
			$stmt->bindValue('s_CatName',$params['CatName']);
			$stmt->execute();						
			$result = $stmt->fetchAll();
			
			unset($stmt);
			
			return $result;
		} catch (Exception $e) {
			return array();
		}
	}
}